package com.fyyg.wechat.controller.order;

import java.util.concurrent.atomic.AtomicInteger;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fyyg.common.redis.RedisBase;
import com.fyyg.common.util.IPAddressUtil;
import com.fyyg.interfaces.TBusiOrderService;
import com.fyyg.model.ApplyOrder;
import com.fyyg.model.GenericResponse;
import com.fyyg.model.ResponseCode;

import redis.clients.jedis.ShardedJedis;


@RequestMapping("/order")
@Controller
public class OrderController{
	
	@Autowired
	TBusiOrderService tBusiOrderService;

	private final AtomicInteger tick = new AtomicInteger();
	
	private static final int THREADS_CONUT = 20;
	//测试使用
//	public static void main(String[] args) {
//		Thread[] threads = new Thread[THREADS_CONUT];
//		for (int i = 0; i < THREADS_CONUT; i++) {
//			threads[i] = new Thread(new Runnable() {
//				@Override
//				public void run() {
//					for (int i = 0; i < 1000; i++) {
//						OrderController api = new OrderController();
//						ApplyOrder applyOrder = new ApplyOrder();
//						System.out.println(api.apply(applyOrder));
//					}
//				}
//			});
//			threads[i].start();
//		}
//
//		while (Thread.activeCount() > 1) {
//			Thread.yield();
//		}
//	}
	
	@RequestMapping("/apply")
	public @ResponseBody GenericResponse<Integer> apply(HttpServletRequest request,@RequestBody ApplyOrder applyOrder){
		try {
			if (waitInLine()) {
				return GenericResponse.fail(ResponseCode.ERROR_DATA_CHECK.getCode(), "系统能力不足，请稍后再试");//告诉用户现在系统处理能力不足，稍候再试;
			}
			// 判断是否重复点击
			if (isFrequently(applyOrder.getOrderId()+"")) {
				return GenericResponse.fail(ResponseCode.ERROR_DATA_CHECK.getCode(), "重复提交，系统正在处理上一次请求");//告诉用户不要重复点击按钮，系统正在处理上一次的请求;
			}
			//业务代码
			applyOrder.setOrderIp(IPAddressUtil.getIpAddress(request));
			
			return GenericResponse.ok(tBusiOrderService.applyOrder(applyOrder));
		}catch(Exception e) {
			return GenericResponse.fail(ResponseCode.ERROR_DEFAULT.getCode(), e.getMessage());
		}finally {
			//一定要写finally
			tick.decrementAndGet();
		}
	}
	
	/**
	 * 判断是否超出处理范围
	 * @return
	 */
	private boolean waitInLine() {
		int in = tick.incrementAndGet();
		System.out.println(in);
		return in > 1000;//表示系统最多同时处理1000个请求
	}
	   /**
	    *	 判断是否提交过频繁
	    */
	public boolean isFrequently(String memberId) {
		ShardedJedis jedis = null;
		try {
			jedis = RedisBase.getInstance().getResource();
			if (StringUtils.isEmpty(jedis.get(memberId))) {
				jedis.set(memberId, "");
				jedis.expire(memberId, 1);// 1秒后标记就消失
			} else {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			RedisBase.getInstance().returnResource(jedis);// 回收连接
		}
		return false;
	}
}
